package cn.edu.tju.wiki.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NodeItem {
	public static int tagIndex = 0;
	public String name;
	public int size = 1;
	public int tag;
	public List<NodeItem> childrenList = new ArrayList<NodeItem>();
	public List<String> keyWordLst;
	public String sourceLstStr="";
	
	private static String nameStr = "{\n\"name\": \"";
	private static String chilStr = "\", \"children\": [\n";
	private static String sizeStr = "\", \"size\": ";
	private static String tagStr = ", \"tag\": ";
	private static String chStr = ", \"ch\": \"";

	public NodeItem(String pname) {
		// TODO Auto-generated constructor stub
		this.name = pname;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static void outputJson(NodeItem rootNode) {
		int subSize = 0;
		int ancSize = 0;

		String resultStr = new String();
		resultStr += nameStr + rootNode.name + chilStr;
		for (int i = 0; i < rootNode.childrenList.size(); i++) {// subchild 1
			resultStr += "\t";
			resultStr += nameStr + rootNode.childrenList.get(i).name + chilStr;

			subSize = 0;
			for (int j = 0; j < rootNode.childrenList.get(i).childrenList
					.size(); j++) {// subchild 2
				resultStr += "\t\t";
				if (j == rootNode.childrenList.get(i).childrenList.size() - 1) {
					resultStr += nameStr
							+ rootNode.childrenList.get(i).childrenList.get(j).name
							+ sizeStr
							+ rootNode.childrenList.get(i).childrenList.get(j).childrenList
									.size() + "}" + "\n";
				} else {
					resultStr += nameStr
							+ rootNode.childrenList.get(i).childrenList.get(j).name
							+ sizeStr
							+ rootNode.childrenList.get(i).childrenList.get(j).childrenList
									.size() + "}," + "\n";
				}
				subSize += rootNode.childrenList.get(i).childrenList.get(j).childrenList
						.size();
			}
			System.out.println(subSize);
			ancSize += subSize;

			resultStr += "\t";
			if (i == rootNode.childrenList.size() - 1) {
				resultStr += "]}\n";
			} else {
				resultStr += "]},\n";
			}
		}
		System.out.println(ancSize);
		resultStr += "]}";
		System.out.println(resultStr);
	}

	
	public static String outPutJsonForWordCloud(NodeItem rootNode) {
		// TODO Auto-generated method stub
		//setNodeSize(rootNode);
		String resultStr = "";
		if(rootNode.childrenList.size() == 0){
			if(" ".equals(rootNode.name)){
				return resultStr;
			}
			resultStr += nameStr + rootNode.name + sizeStr + rootNode.size + 
					tagStr + rootNode.tag + chStr + rootNode.sourceLstStr+"\"},\n";
		}else{
			resultStr += "\t" + nameStr + rootNode.name + chilStr;
			for (int i = 0; i < rootNode.childrenList.size(); i++) {
				resultStr += "\t" + outPutJsonForWordCloud(rootNode.childrenList.get(i));
			}
			resultStr = resultStr.substring(0, resultStr.length()-2);
			resultStr += "]},\n";
		}
		return resultStr;
	}
	
	private static int setNodeSize(NodeItem rootNode) {
		// TODO Auto-generated method stub
		if(rootNode.childrenList.size() == 0){
			
			return rootNode.size;
		}else{
			for (int i = 0; i < rootNode.childrenList.size(); i++) {
				rootNode.size += setNodeSize(rootNode.childrenList.get(i));
			}
			return rootNode.size;
		}
	}
	
	public static int setNodeSizeAndCH(NodeItem rootNode, List<Map<String, Object>> relicsInfo) {
		// TODO Auto-generated method stub
		Constant.oriNodeLst.add(rootNode);
		if(rootNode.childrenList.size() == 0){
			if(" ".equals(rootNode.name)){
				return rootNode.size;
			}
			rootNode.tag = NodeItem.tagIndex;
			for (int i = 0; i < relicsInfo.size(); i++) {
				List<String> relicInf = (List<String>) relicsInfo.get(i).get("keywords");
				for (int j = 0; j < relicInf.size(); j++) {
//					System.out.println(j+"---------"+rootNode.name+"--------------"+relicInf.get(j));
					if(j == 1){
						if(relicInf.get(j).contains(rootNode.name)){
//							System.out.println(rootNode.name);
							rootNode.sourceLstStr += i+"_";
						}
					}else{
						if(rootNode.name.equals(relicInf.get(j))){
//							System.out.println(rootNode.name);
							rootNode.sourceLstStr += i+"_";
						}
					}
				}
			}
			return rootNode.size;
		}else{
			NodeItem.tagIndex++;
			for (int i = 0; i < rootNode.childrenList.size(); i++) {
				rootNode.size += setNodeSizeAndCH(rootNode.childrenList.get(i), relicsInfo);
			}
			return rootNode.size;
		}
	}
}
